package dk.simonvogensen.itodo;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

public class ITodoActivity extends Activity {

    public static final String PREFS_NAME = "itodoPrefsFile";

    public static final String ITEM_TEXT = "itodoItemText";
    public static final String ITEM_INDEX = "itodoItemIndex";
    public static final String ITEM_LATEST_INDEX = "itodoItemLatestIndex";
    public static final String AUTH_COOKIE = "itodoAuthCookie";
    private static final String AUTH_EMAIL = "itodoAuthEmail";
    private static final String AUTH_PASSWD = "itodoAuthPasswd";

    private static final int DIALOG_LOGIN_ID = 0;

    final GetDataFromServlet dataFromServlet = new GetDataFromServlet();
    private int mIndex;
    private int mLatestIndex;

    private String mCookie;
    private EditText mText;
    private Button prevButton;
    private Button nextButton;
    private String mEmail;
    private String mPasswd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.note_edit);

        mText = (EditText) findViewById(R.id.body);
        nextButton = (Button) findViewById(R.id.next);
        prevButton = (Button) findViewById(R.id.prev);

        // Restore preferences
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        mEmail = settings.getString(AUTH_EMAIL, null);
        mPasswd = settings.getString(AUTH_PASSWD, null);

        mCookie = savedInstanceState != null ? savedInstanceState.getString(AUTH_COOKIE) : null;
        mIndex = savedInstanceState != null ? savedInstanceState.getInt(ITEM_INDEX) : -1;
        mLatestIndex = savedInstanceState != null ? savedInstanceState.getInt(ITEM_LATEST_INDEX) : -1;
        String itemText = savedInstanceState != null ? savedInstanceState.getString(ITEM_TEXT) : "";

        mText.setFocusable(false);
        mText.setText(itemText);

        nextButton.setEnabled(mIndex < mLatestIndex);
        prevButton.setEnabled(mIndex > 0);
        prevButton.setSelected(true);

        if(mCookie == null) {
            showDialog(DIALOG_LOGIN_ID);
        } else {
            dataFromServlet.execute(ITodoActivity.this, Command.LatestItem, mIndex, mCookie, null, null);
        }

        nextButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                dataFromServlet.execute(ITodoActivity.this, Command.NextItem, mIndex+1, mCookie, null, null);
            }
        });

        prevButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                dataFromServlet.execute(ITodoActivity.this, Command.PrevItem, mIndex-1, mCookie, null, null);
            }
        });
    }

    protected Dialog onCreateDialog(int id) {
        final Dialog dialog;
           switch(id) {
           case DIALOG_LOGIN_ID:
               dialog = new Dialog(this);
               dialog.setContentView(R.layout.login_dialog);
               dialog.setTitle("Login Dialog");
               Button okButton = (Button) dialog.findViewById(R.id.login_ok);
               Log.d("ITODO", ""+(okButton == null));
               final EditText emailEditText = (EditText) dialog.findViewById(R.id.login_email);
               final EditText passwdEditText = (EditText) dialog.findViewById(R.id.login_passwd);
               emailEditText.setMinimumWidth(200);
               emailEditText.setText(mEmail);
               passwdEditText.setText(mPasswd);
               
               okButton.setOnClickListener(new View.OnClickListener() {
                   public void onClick(View view) {
                       mEmail = emailEditText.getText().toString();
                       mPasswd = passwdEditText.getText().toString();
                       dialog.dismiss();
                       dataFromServlet.execute(ITodoActivity.this, Command.LatestItem, mIndex, mCookie, mEmail, mPasswd);
                   }
               });
               Button cancelButton = (Button) dialog.findViewById(R.id.login_cancel);
               cancelButton.setOnClickListener(new View.OnClickListener() {
                   public void onClick(View view) {
                       dialog.dismiss();
                   }
               });

               break;
           default:
               dialog = null;
           }
           return dialog;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(ITEM_INDEX, mIndex);
        outState.putInt(ITEM_LATEST_INDEX, mIndex);
        outState.putString(AUTH_COOKIE, mCookie);
        outState.putString(ITEM_TEXT, mText.getText().toString());
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       SharedPreferences.Editor editor = settings.edit();
       editor.putString(AUTH_EMAIL, mEmail);
        editor.putString(AUTH_PASSWD, mPasswd);
       editor.commit();
    }

    public void displayUserProfile(int index, int latestIndex, String data, String cookie) {
        mText.setText(data);
        mIndex = index;
        mLatestIndex = latestIndex;
        mCookie = cookie;
        nextButton.setEnabled(mIndex < mLatestIndex);
        prevButton.setEnabled(mIndex > 0);
    }
}
